package com.zhang.web.services;

import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;

import com.zhang.pojo.Order;
import com.zhang.pojo.Shop;

public class ShopsDAO {
	
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<Shop> AllShops(){
		List<Shop> list = this.sessionFactory.openSession()
				.createQuery("from Shop").list();
		if(list.size()>0)
			return list;
		else
			return null;
	
	}
	
	public List<Shop> ShopByName(String name){
		List<Shop> list = this.sessionFactory.openSession()
				.createQuery("from Shop shop where shop.name=?").setString(0, name).list();
		if(list.size()>0)
			return list;
		else
			return null;
	}
	
	public Shop ShopByID(int id){
		List list = this.sessionFactory.openSession()
				.createQuery("from Shop shop where shop.id=?").setLong(0, id).list();
		if(list.size()>0)
			return (Shop) list.get(0);
		else
			return null;
	}
	
	public List<Shop> ShopsByOrder(){
		List<Shop> list = this.sessionFactory.openSession()
				.createQuery("from Shop shop left join shop.orders as ords order by count(ords)").list();
		if(list.size()>0)
			return list;
		else
			return null;
	
	}
	
	public Set<Order> getOrders(String shop_name){
		List<Shop> list = ShopByName(shop_name);
		if(list.size()>0)
			return list.get(0).getOrders();
		else
			return null;
	
	}
	
}
